<div class="row animate fadeIn">
    <form id="addLookUpForm" class="form-horizontal"  method="post">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add</i></h5>
            <div class="ibox-tools">
                <button type="button" id="saveLookUp" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
                <a id="done_btn" style="display: none;" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Finish</a>
            </div>
        </div>
        <div class="ibox-content">
            <div class="form-group"><label class="col-lg-3 control-label">Email Address</label>
                <div class="col-lg-10 input-s">
                  <select class="form-control m-b chosen" name="email">
                      <option value="">Select Email</option>
                      <?php foreach ($email as $e):?>
                        <?php if(!empty($e->email)): ?>
                          <option value ="<?php echo $e->user_id; ?>"><?php echo $e->email; ?></option>
                        <?php endif; ?>
                      <?php endforeach; ?>
                  </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Enabled</label>
                <div class="col-lg-10 input-s">
                    <input type="checkbox" name="enable_flag" value="ok" class="i-checks" checked>
                </div>
            </div>
    </form>
        </div>
         <div id="add-value" style="display:none;">
           <div class="ibox-title no-top-border">
          </div>
          <div class="ibox-content"><form id="report_form">
            <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>Report</th>
                    <th>File Type</th>
                    <th colspan="2">Schedule</th>
                    <th>Enable Flag</th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <input type="hidden" name="1email_id">
                    <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <select name="1report_id" class="form-control m-b chosen">
                        <option value="">Select Report</option>
                        <?php foreach($report as $r): ?>
                          <option value="<?php echo $r->lookup_code; ?>"><?php echo $r->meaning; ?></option>
                        <?php endforeach; ?>
                      </select>
                    </td>
                    <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <select name="1filetype" class="form-control m-b chosen">
                        <option value="pdf">PDF</option>
                        <option value="excel">Excel</option>
                      </select>
                    </td>
                    <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <select name="1times" class="form-control m-b chosen">
                        <option value="once">Once</option>
                        <option value="twice">Twice</option>
                      </select>
                    </td>
                    <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <select name="1schedule" class="form-control m-b chosen">
                        <?php foreach($schedule as $s): ?>
                          <option value="<?php echo $s->lookup_code; ?>"><?php echo $s->meaning; ?></option>
                        <?php endforeach; ?>
                      </select>
                    </td>
                    <!-- <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <select name="time" class="form-control m-b chosen1">
                        <?php for($x = 1; $x <= 12; $x++): ?>
                        <option value="<?php echo $x; ?>"><?php echo $x; ?>:00</option>
                      <?php endfor; ?>
                      </select>
                    </td>
                    <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                    <select name="ampm"  class="form-control m-b chosen1">
                        <option value="AM">AM</option>
                        <option value="PM">PM</option>
                      </select>
                    </td> -->
                    <td align="center" style="padding-top: 6px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <input type="checkbox" value="ok" checked name="1enable_flag" class="i-checks">
                    </td>
                    <!-- <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <button title="Add" style="width: 100%" class="btn btn-white" >Parameter</button>
                    </td> -->
                    <td style="padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;">
                      <button title="Add" style="width: 100%" class="btn btn-white" id="save_report"><i class="fa fa-plus success"></i></button>
                    </td>
                    
                  </tr>
                </tbody>
                </form>
                <tbody id="luva">
                  
                </tbody>
            </table>
        </div>
         </div>
</div>
<div class="modal fade" id="report_modal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
           <h4 class="modal-title" id="myModalLabel">Edit Report Detail</h4>
        </div>
      <div class="modal-body">
        <div class="row">
          <form id="form_report_dtl_upd" method="post" class="form-horizontal">
              <input type="hidden" name="email_dtl_id" value="">
              <div class="form-group">
                  <label class="col-lg-3 control-label">Report Name</label>
                  <div class="col-lg-5">
                       <select name="report_id" class="form-control m-b chosen">
                        <option value="">Select Report</option>
                        <?php foreach($report as $r): ?>
                          <option value="<?php echo $r->lookup_code; ?>"><?php echo $r->meaning; ?></option>
                        <?php endforeach; ?>
                      </select>
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Report Name</label>
                  <div class="col-lg-5">
                       <select name="filetype" class="form-control m-b chosen">
                        <option value="pdf">PDF</option>
                        <option value="excel">Excel</option>
                      </select>
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Value</label>
                  <div class="col-lg-5">
                      <select name="times" class="form-control m-b chosen">
                        <option value="once">Once</option>
                        <option value="twice">Twice</option>
                      </select>
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Schedule</label>
                  <div class="col-lg-5">
                      <select name="schedule" class="form-control m-b chosen">
                        <?php foreach($schedule as $s): ?>
                          <option value="<?php echo $s->lookup_code; ?>"><?php echo $s->meaning; ?></option>
                        <?php endforeach; ?>
                      </select>
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Enable FLag</label>
                  <div class="col-lg-5">
                      <input type="checkbox" value="ok" name="enable_flag" class="i-checks">
                  </div>
              </div>
          </form>
        </div>
      </div>
      <div class="modal-footer">
           <p class="text-danger" id="flashmsg"></p>
           <button id="save_report_dtl_upd" type="button" class="btn btn-primary" >Save</button>
      </div>
    </div>
  </div>
</div> <!-- end-of-modal -->
      <script>
            $(document).ready(function () {
              $('.clockpicker').clockpicker();
              $(".chosen").chosen({width:"100%;"});
              $(".chosen1").chosen({width:"100%;","disable_search": true});
              $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
              });

               $("#addLookUpForm").validate({
                rules:{
                    email: {
                        required: true
                    }
                  }
                });

               $("#report_form").validate({
                rules:{
                    report_id: {
                        required: true
                    },
                    times: {
                        required: true
                    },
                    schedule: {
                        required: true
                    }
                  }
                });

          $(document).on('click','.open_modal',function(){
            var form_id = $(this).closest('tr').children('input[name=form_id]').val();
            var email_dtl_id = $(this).closest('tr').children('input[name=email_dtl_id]').val();
            var times = $(this).closest('tr').children('input[name=times]').val();
            var lookup_code = $(this).closest('tr').children('input[name=lookup_code]').val();
            var ena_flag = $(this).closest('tr').children('input[name=enable_flag]').val();
            var filetype = $(this).closest('tr').children('input[name=filetype]').val();
            $('#report_modal input[name=email_dtl_id]').val(email_dtl_id);
            $('#report_modal select[name=report_id]').val(form_id);
            $('#report_modal select[name=times]').val(times);
            $('#report_modal select[name=schedule]').val(lookup_code);
            $('#report_modal select[name=filetype]').val(filetype);
            //$('#report_modal input[name=enable_flag]').attr('checked', (ena_flag == 'Y' ? true : false));
            $('#report_modal input[name=enable_flag]').iCheck((ena_flag == 'Y' ? 'check' : 'uncheck'));
            $(".chosen").trigger('chosen:updated');
            console.log(email_dtl_id);
            console.log(lookup_code);
            console.log(times);
            console.log(ena_flag);
         });

        $('#save_report_dtl_upd').click(function(){
            $('#flashmsg').html('');
            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/updateReportDtl'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#form_report_dtl_upd').serialize(),
                success: function(e) {
                  console.log(e);
                  $("#save_report_dtl_upd").removeAttr('disabled');
                  if(e.err == 0){
                    notifymsg("success","Updated Successfully!");
                    getReportDtl();
                    $('#report_modal').modal('hide');
                  }else{
                    $('#flashmsg').html(e.msg);
                    notifymsg("danger",e.msg);
                  }
                },
                beforeSend: function(){
                  $("#save_report_dtl_upd").attr('disabled',true);
                }
              });
        });

               $("#saveLookUp").click(function(){
                 if($("#addLookUpForm").valid() == true ){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insert'); ?>",
                        data: $('#addLookUpForm').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 1){
                            $("#saveLookUp").removeAttr('disabled');
                            notifymsg("danger",data.msg);
                          }else{
                            notifymsg("success","Added Successfully!");
                            $("#report_form input[name=1email_id]").val(data.id);
                            $("#addLookUpForm input[name=lookup_type]").attr('disabled',true);
                            $("#addLookUpForm #done_btn").show();
                            $("#addLookUpForm #cancel_add").hide();
                            $("#luvFormAdda input[name=LookUpType]").val(data.code);
                            $('#add-value').show();
                          }
                        },
                        beforeSend: function(){
                           $("#saveLookUp").attr('disabled',true);
                        }
                    });
                 }
               });

               function getReportDtl(){
                  var id = $('#report_form input[name=1email_id]').val();
                  $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/reportdtl'); ?>/"+id,
                        dataType: 'json',
                        success: function(data){
                          var tmp = '';
                            $.each(data.file, function(i, item) {
                              tmp += '<tr><input type="hidden" name="form_id" value="'+ data.file[i].form_id +'"/><input type="hidden" name="lookup_code" value="'+ data.file[i].lookup_code +'"/><input type="hidden" name="enable_flag" value="'+ data.file[i].enable_flag +'"/><input type="hidden" name="enable_flag" value="'+ data.file[i].enable_flag +'"/><input type="hidden" name="filetype" value="'+ data.file[i].filetype +'"/><input type="hidden" name="times" value="'+ data.file[i].times +'"/><td>'+ data.file[i].form_name +'</td><td>'+ data.file[i].filetype +'</td><td id="timesss">'+ data.file[i].times +'</td><td>'+ data.file[i].meaning +'</td><td style="display:none;">'+ data.file[i].time +':00</td><td style="display:none;">'+ data.file[i].ampm +'</td> <td align="center" style="padding-top: 6px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;"><div class="state icheckbox_square-green '+ (data.file[i].enable_flag == 'Y' ? 'checked' : 'hover') +'"></div></td><td align="center"><button type="button" class="btn btn-outline btn-sm btn-primary btn-xs open_modal" data-toggle="modal" title="Edit" data-target="#report_modal"><i class="fa fa-pencil fa-lg"></i></button></td></tr>';
                            });
                            $("#luva").html(tmp);
                            $(".chosen").trigger('chosen:updated');
                          }
                    });
               }

               $("#save_report").click(function(){
                 if($("#report_form").valid() == true){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insertReport'); ?>",
                        data: $('#report_form').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 1){
                            $("#save_report").removeAttr('disabled');
                            notifymsg("danger",data.msg);
                          }else{
                            var tmp = "";
                            notifymsg("success","Added Successfully!");
                            $("#save_report").removeAttr('disabled');
                            $.each(data.file, function(i, item) {
                              tmp += '<tr><input type="hidden" name="form_id" value="'+ data.file[i].form_id +'"/><input type="hidden" name="lookup_code" value="'+ data.file[i].lookup_code +'"/><input type="hidden" name="filetype" value="'+ data.file[i].filetype +'"/><input type="hidden" name="enable_flag" value="'+ data.file[i].enable_flag +'"/><input type="hidden" name="email_dtl_id" value="'+ data.file[i].email_dtl_id +'"/><input type="hidden" name="times" value="'+ data.file[i].times +'"/><td>'+ data.file[i].form_name +'</td><td>'+ data.file[i].filetype +'</td><td id="timesss">'+ data.file[i].times +'</td><td>'+ data.file[i].meaning +'</td><td style="display:none;">'+ data.file[i].time +':00</td><td style="display:none;">'+ data.file[i].ampm +'</td> <td align="center" style="padding-top: 6px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;"><div class="state icheckbox_square-green '+ (data.file[i].enable_flag == 'Y' ? 'checked' : 'hover') +'"></div></td><td align="center"><button type="button" class="btn btn-outline btn-sm btn-primary btn-xs open_modal" data-toggle="modal" title="Edit" data-target="#report_modal"><i class="fa fa-pencil fa-lg"></i></button></td></tr>';
                            });
                            $("#luva").html(tmp);
                            $(".chosen").trigger('chosen:updated');
                          }
                        },
                        beforeSend: function(){
                           $("#save_report").attr('disabled',true);
                        }
                    });
                 }
               });

               $("#luvFormAdda").validate({
                     rules: {
                         LookUpCode: {
                             required: true
                         }
                     }
                });

              $(document).on('click',"#store_lookupvaluesa",function(){
              if($("#luvFormAdda").valid() == true){
                $.ajax({
                  type: "POST",
                  url: "<?php echo base_url($this->session->userdata('forajax') . '/insertLookUpValues'); ?>",
                  data: $("#luvFormAdda").serialize(),
                  dataType: 'json',
                  success: function(data){
                    if(data.err == 1){
                      getLookUpValuesa();
                      $('#store_lookupvaluesa').removeAttr('disabled');
                      $('#addNewa').modal('hide');
                      notifymsg("danger",data.msg);
                    }else{
                      getLookUpValuesa();
                      $('#addNewa').modal('hide');
                      notifymsg("success"," . . .Add Succesful");
                      $("#luvFormAdda")[0].reset();
                      $('#store_lookupvaluesa').removeAttr('disabled');
                    }
                  },
                  beforeSend: function(){
                    $('#store_lookupvaluesa').attr('disabled',true);
                  }
                });
              }
            });

            $('#cancel_add').click(function () {
                              swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                              close_add(); active_search();
                                              notifymsg("hide","hide");
                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                          });

            });
        $('#addX').click(function(){
            close_add();
       });
        $('#done_btn').click(function(){
            close_add();
       });
</script>